﻿/*=====================================================================
  
    File:      TabularDatabase.Perspective.cs

    Summary:   This is the class that implements all related functionality 
               around Perspectives in a tabular model, for all TabularDatabase 
               commandlets.
 
               Tabular Database Cmdlets (TabularDatabaseCmdlets) is sample code 
               to show and explain how to use the AMO to Tabular (AMO2Tabular)
               library to build a PowerShell library of cmdlets to manage 
               Tabular model objects. 
               The sample can be seen as a sample library of cmdlets
               with the necessary code to execute each particular 
               action or operation over a logical tabular object. 

    Authors:   JuanPablo Jofre (jpjofre@microsoft.com)
    Date:	   04-Apr-2012
  
    Change history:

    @TODO: 
  
-----------------------------------------------------------------------
  
    This file is part of the Microsoft SQL Server Code Samples.
    Copyright (C) Microsoft Corporation.  All rights reserved.
  
  This source code is intended only as a supplement to Microsoft
  Development Tools and/or on-line documentation.  
  
  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF 
  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  PARTICULAR PURPOSE.
  
======================================================================*/


using System;
using System.Globalization;
using System.Management.Automation;     //  Windows PowerShell assembly.
using MicrosoftSql2012Samples.Amo2Tabular;
using MicrosoftSql2012Samples.TabularDatabaseCmdlet.Properties;
using AMO = Microsoft.AnalysisServices;

namespace MicrosoftSql2012Samples.TabularDatabaseCmdlet
{
    [Cmdlet(VerbsCommon.Add, "PerspectiveToTabularDatabase")]
    public class AddPerspectiveToTabularDatabase : TabularDatabaseBase
    {
        //  Declare mandatory parameters

        [Parameter(Mandatory = true, Position = 2)]
        [ValidateNotNullOrEmpty]
        public string perspectiveName { get; set; }

        // Overide the ProcessRecord method to add a Perspective
        // using the supplied server name and database name 
        protected override void ProcessRecord()
        {
            using (AMO.Server server = new AMO.Server())
            {
                try
                {
                    server.Connect(serverName);
                    using (AMO.Database database = server.Databases.GetByName(databaseName))
                    {
                        AMO2Tabular.PerspectiveAdd(database, perspectiveName, true);
                        WriteVerbose(string.Format(CultureInfo.InvariantCulture, Resources.AddPerspectiveToTabularDatabaseWriteReport, perspectiveName, databaseName));
                    }
                }
                catch (Exception ex)
                {
                    ThrowTerminatingError(new ErrorRecord(ex, string.Format(CultureInfo.InvariantCulture, Resources.AddTabularFirstTableTerminatingError, databaseName, serverName), ErrorCategory.InvalidOperation, null));
                }
            }

        }

    }

    [Cmdlet(VerbsCommon.Add, "TableToTabularPerspective")]
    public class AddTableToTabularPerspective : TabularDatabaseBase
    {
        //  Declare mandatory parameters

        [Parameter(Mandatory = true, Position = 2)]
        [ValidateNotNullOrEmpty]
        public string perspectiveName { get; set; }

        [Parameter(Mandatory = true, Position = 3)]
        [ValidateNotNullOrEmpty]
        public string tableName { get; set; }

        // Overide the ProcessRecord method to add a Perspective
        // using the supplied server name and database name 
        protected override void ProcessRecord()
        {
            using (AMO.Server server = new AMO.Server())
            {
                try
                {
                    server.Connect(serverName);
                    using (AMO.Database database = server.Databases.GetByName(databaseName))
                    {
                        AMO2Tabular.PerspectiveAlterTableAdd(database, perspectiveName, tableName, true);
                        WriteVerbose(string.Format(CultureInfo.InvariantCulture, Resources.AddTableToTabularPerspectiveWriteReport, tableName, perspectiveName));
                    }
                }
                catch (Exception ex)
                {
                    ThrowTerminatingError(new ErrorRecord(ex, string.Format(CultureInfo.InvariantCulture, Resources.AddTabularFirstTableTerminatingError, databaseName, serverName), ErrorCategory.InvalidOperation, null));
                }
            }

        }

    }

    [Cmdlet(VerbsCommon.Add, "ColumnToTabularPerspective")]
    public class AddColumnToTabularPerspective : TabularDatabaseBase
    {
        //  Declare mandatory parameters

        [Parameter(Mandatory = true, Position = 2)]
        [ValidateNotNullOrEmpty]
        public string perspectiveName { get; set; }

        [Parameter(Mandatory = true, Position = 3)]
        [ValidateNotNullOrEmpty]
        public string tableName { get; set; }

        [Parameter(Mandatory = true, Position = 4)]
        [ValidateNotNullOrEmpty]
        public string columnName { get; set; }

        // Overide the ProcessRecord method to add a Perspective
        // using the supplied server name and database name 
        protected override void ProcessRecord()
        {
            using (AMO.Server server = new AMO.Server())
            {
                try
                {
                    server.Connect(serverName);
                    using (AMO.Database database = server.Databases.GetByName(databaseName))
                    {
                        AMO2Tabular.PerspectiveAlterColumnAdd(database, perspectiveName, tableName, columnName, true);
                        WriteVerbose(string.Format(CultureInfo.InvariantCulture, Resources.AddColumnToTabularPerspectiveWriteReport, columnName, perspectiveName));
                    }
                }
                catch (Exception ex)
                {
                    ThrowTerminatingError(new ErrorRecord(ex, string.Format(CultureInfo.InvariantCulture, Resources.AddTabularTableTerminatingError, databaseName, serverName), ErrorCategory.InvalidOperation, null));
                }
            }

        }

    }

    [Cmdlet(VerbsCommon.Add, "MeasureToTabularPerspective")]
    public class AddMeasureToTabularPerspective : TabularDatabaseBase
    {
        //  Declare mandatory parameters

        [Parameter(Mandatory = true, Position = 2)]
        [ValidateNotNullOrEmpty]
        public string perspectiveName { get; set; }

        [Parameter(Mandatory = true, Position = 3)]
        [ValidateNotNullOrEmpty]
        public string tableName { get; set; }

        [Parameter(Mandatory = true, Position = 4)]
        [ValidateNotNullOrEmpty]
        public string measureName { get; set; }

        // Overide the ProcessRecord method to add a Perspective
        // using the supplied server name and database name 
        protected override void ProcessRecord()
        {
            using (AMO.Server server = new AMO.Server())
            {
                try
                {
                    server.Connect(serverName);
                    using (AMO.Database database = server.Databases.GetByName(databaseName))
                    {
                        AMO2Tabular.PerspectiveAlterColumnAdd(database, perspectiveName, tableName, measureName, true);
                        WriteVerbose(string.Format(CultureInfo.InvariantCulture, Resources.AddMeasureToTabularPerspectiveWriteReport, measureName, perspectiveName));
                    }
                }
                catch (Exception ex)
                {
                    ThrowTerminatingError(new ErrorRecord(ex, string.Format(CultureInfo.InvariantCulture, Resources.AddTabularTableTerminatingError, databaseName, serverName), ErrorCategory.InvalidOperation, null));
                }
            }

        }

    }

    [Cmdlet(VerbsCommon.Add, "HierarchyToTabularPerspective")]
    public class AddHIerarchyToTabularPerspective : TabularDatabaseBase
    {
        //  Declare mandatory parameters

        [Parameter(Mandatory = true, Position = 2)]
        [ValidateNotNullOrEmpty]
        public string perspectiveName { get; set; }

        [Parameter(Mandatory = true, Position = 3)]
        [ValidateNotNullOrEmpty]
        public string tableName { get; set; }

        [Parameter(Mandatory = true, Position = 4)]
        [ValidateNotNullOrEmpty]
        public string hierarchyName { get; set; }

        // Overide the ProcessRecord method to add a Perspective
        // using the supplied server name and database name 
        protected override void ProcessRecord()
        {
            using (AMO.Server server = new AMO.Server())
            {
                try
                {
                    server.Connect(serverName);
                    using (AMO.Database database = server.Databases.GetByName(databaseName))
                    {
                        AMO2Tabular.PerspectiveAlterColumnAdd(database, perspectiveName, tableName, hierarchyName, true);
                        WriteVerbose(string.Format(CultureInfo.InvariantCulture, Resources.AddHierarchyToTabularPerspectiveWriteReport, hierarchyName, perspectiveName));
                    }
                }
                catch (Exception ex)
                {
                    ThrowTerminatingError(new ErrorRecord(ex, string.Format(CultureInfo.InvariantCulture, Resources.AddTabularTableTerminatingError, databaseName, serverName), ErrorCategory.InvalidOperation, null));
                }
            }

        }

    }

    [Cmdlet(VerbsDiagnostic.Test, "ColumnExistsInTabularPerspective")]
    public class TestColumnExistsInTabularPerspective : TabularDatabaseBase
    {
        //  Declare mandatory parameters

        [Parameter(Mandatory = true, Position = 2)]
        [ValidateNotNullOrEmpty]
        public string perspectiveName { get; set; }

        [Parameter(Mandatory = true, Position = 3)]
        [ValidateNotNullOrEmpty]
        public string tableName { get; set; }

        [Parameter(Mandatory = true, Position = 4)]
        [ValidateNotNullOrEmpty]
        public string columnName { get; set; }

        // Overide the ProcessRecord method to Test Column Existence
        // using the supplied server name and database name 
        protected override void ProcessRecord()
        {

            using (AMO.Server server = new AMO.Server())
            {
                try
                {
                    server.Connect(serverName);
                    using (AMO.Database database = server.Databases.GetByName(databaseName))
                    {
                        WriteObject(AMO2Tabular.PerspectiveContainsColumn(database, perspectiveName, tableName, columnName), true);
                    }
                }
                catch (Exception ex)
                {
                    ThrowTerminatingError(new ErrorRecord(ex, string.Format(CultureInfo.InvariantCulture, Resources.AddTabularTableTerminatingError, databaseName, serverName), ErrorCategory.InvalidOperation, null));
                }
            }

        }

    }

    [Cmdlet(VerbsDiagnostic.Test, "MeasureExistsInTabularPerspective")]
    public class TestMeasureExistsInTabularPerspective : TabularDatabaseBase
    {
        //  Declare mandatory parameters

        [Parameter(Mandatory = true, Position = 2)]
        [ValidateNotNullOrEmpty]
        public string perspectiveName { get; set; }

        [Parameter(Mandatory = true, Position = 3)]
        [ValidateNotNullOrEmpty]
        public string measureName { get; set; }

        // Overide the ProcessRecord method to Test Measure Existence
        // using the supplied server name and database name 
        protected override void ProcessRecord()
        {

            using (AMO.Server server = new AMO.Server())
            {
                try
                {
                    server.Connect(serverName);
                    using (AMO.Database database = server.Databases.GetByName(databaseName))
                    {
                        WriteObject(AMO2Tabular.PerspectiveContainsMeasure(database, perspectiveName, measureName), true);
                    }
                }
                catch (Exception ex)
                {
                    ThrowTerminatingError(new ErrorRecord(ex, string.Format(CultureInfo.InvariantCulture, Resources.AddTabularTableTerminatingError, databaseName, serverName), ErrorCategory.InvalidOperation, null));
                }
            }

        }

    }

    [Cmdlet(VerbsDiagnostic.Test, "HierarchyExistsInTabularPerspective")]
    public class TestHierarchyExistsInTabularPerspective : TabularDatabaseBase
    {
        //  Declare mandatory parameters

        [Parameter(Mandatory = true, Position = 2)]
        [ValidateNotNullOrEmpty]
        public string perspectiveName { get; set; }

        [Parameter(Mandatory = true, Position = 3)]
        [ValidateNotNullOrEmpty]
        public string tableName { get; set; }

        [Parameter(Mandatory = true, Position = 4)]
        [ValidateNotNullOrEmpty]
        public string hierarchyName { get; set; }

        // Overide the ProcessRecord method to Test Hierarchy Existence
        // using the supplied server name and database name 
        protected override void ProcessRecord()
        {

            using (AMO.Server server = new AMO.Server())
            {
                try
                {
                    server.Connect(serverName);
                    using (AMO.Database database = server.Databases.GetByName(databaseName))
                    {
                        WriteObject(AMO2Tabular.PerspectiveContainsColumn(database, perspectiveName, tableName, hierarchyName), true);
                    }
                }
                catch (Exception ex)
                {
                    ThrowTerminatingError(new ErrorRecord(ex, string.Format(CultureInfo.InvariantCulture, Resources.AddTabularTableTerminatingError, databaseName, serverName), ErrorCategory.InvalidOperation, null));
                }
            }

        }

    }

}
